package com.foreveross.weixin.service;

import com.foreveross.util.XmlUtil;
import com.foreveross.weixin.common.Constant;
import com.foreveross.weixin.util.ObjectToMap;
import com.foreveross.weixin.util.SignUtil;
import com.foreveross.weixin.util.StringUtil;
import com.foreveross.weixin.util.WeixinUtil;
import com.foreveross.weixin.vo.pay.PayRefund;
import com.foreveross.weixin.vo.pay.PayRefundResult;
/**
 * 退款Service
 * @author Administrator
 *
 */
public class PayRefundService {
	
	/**
	 * 退款申请
	 * @param appId
	 * @param mchId
	 * @param certUrl
	 * @param outTradeNo
	 * @param total_fee
	 * @return
	 * @throws Exception
	 */
	public static PayRefundResult getPayRefundResult(
			String appId, String mchId,String apiKey,String certUrl, String outTradeNo,String total_fee) throws Exception{
		String nonceStr = StringUtil.create_nonce_str();
		PayRefund payRefund = new PayRefund();
		payRefund.setAppid(appId);
		payRefund.setMch_id(mchId);
		payRefund.setNonce_str(nonceStr);
		payRefund.setOut_trade_no(outTradeNo);
		payRefund.setOut_refund_no(StringUtil.getTradeNo());
		payRefund.setTotal_fee(total_fee);
		payRefund.setRefund_fee(total_fee);
		payRefund.setOp_user_id(mchId);

		String sign = SignUtil.getPayCustomSign(ObjectToMap.objectToMapViaFields(payRefund, false), apiKey);
		payRefund.setSign(sign);
		String xmlStr = XmlUtil.ClazzToXml(payRefund);
		System.out.println(xmlStr);

		PayRefundResult payRefundResult = WeixinUtil.getPayRefundResult(xmlStr, certUrl, mchId);
		return payRefundResult;
	}
	
	
	public static void main(String[] args) {
		try {
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
